﻿using UnityEngine;
using WebSocketSharp;
using WebSocketSharp.Net;
using SocketIO;

public class NetworkModule
{
   private static NetworkModule _instance;
   private GameObject oldObj;
   public static NetworkModule Instance {
        get {
            if(_instance == null)
            {
                _instance = new NetworkModule();
            }
            return _instance;
        }
    }
    public string srvUrl = "ws://sh.grissom.win:3000/socket.io/?EIO=3&transport=websocket";
    private SocketIO.SocketIOComponent socket;

    public NetworkModule()
    {
        this.Init();
    }

    public void Init()
    {
        Debug.Log("Init Network Module....");
        GameObject go = GameObject.Find("SocketIO");
        if(oldObj == go){
            return;
        }
        oldObj = go;
        socket = go.GetComponent<SocketIOComponent>();
        //socket = new SocketIO.SocketIOComponent();
        //socket.url = srvUrl;
        //socket.autoConnect = true;
        //socket.reconnectDelay = 5;
        //socket.ackExpirationTime = 1800;
        //socket.pingInterval = 25;
        //socket.pingTimeout = 60;
        socket.On("open", TestOpen);
        socket.On("error", TestError);
        socket.On("close", TestClose);
        socket.On("message", TestOnMessage);
        Debug.Log("start ...");
    }
    void onMessageHandler(object sender, MessageEventArgs e)
    {
        Debug.Log("Laputa says: " + e.Data);
    }
    public void TestOpen(SocketIOEvent e)
    {
        Debug.Log("[SocketIO] Open received: " + e.name + " " + e.data);
        socket.Emit("screen");
    }

    public void TestBoop(SocketIOEvent e)
    {
        Debug.Log("[SocketIO] Boop received: " + e.name + " " + e.data);

        if (e.data == null) { return; }

        Debug.Log(
            "#####################################################" +
            "THIS: " + e.data.GetField("this").str +
            "#####################################################"
        );
    }

    public void TestError(SocketIOEvent e)
    {
        Debug.Log("[SocketIO] Error received: " + e.name + " " + e.data);
    }
    public delegate void OnRecvMessageHandler(Message msg);
    public OnRecvMessageHandler OnRecvMessage;
    public void TestOnMessage(SocketIOEvent e)
    {
        Debug.Log("[SocketIO] Error received Msg: " + e.name + " " + e.data);
        Debug.Log("[SocketIO] Error received Msg: " + e.name + " content " + e.data);
        Message data = JsonUtility.FromJson<Message>(e.data.ToString());
        Debug.Log(data.nickname + "---" + data.content);
        if(OnRecvMessage != null)
        {
            OnRecvMessage(data);
        }
    }
    public void TestClose(SocketIOEvent e)
    {
        Debug.Log("[SocketIO] Close received: " + e.name + " " + e.data);
    }
    
    public void SendChapterChangeMessage(int chapterId)
    {
        //string js = JsonUtility.ToJson<Message>(msg);
        JSONObject jo = JSONObject.Create(chapterId);
        socket.Emit("chapter", jo);
    }

}
